<?php
/**
 * Created by PhpStorm.
 * User: songshuai@foxmail.com
 * Date: 2020/7/13
 * Time: 14:50
 */

namespace app\components\Api;

use app\components\Fun;
use app\components\Pay\PayService;
use app\models\money\MoneyBalance;
use app\models\money\MoneyPay;
use Yii;
use app\components\API;
use app\models\User;

class Pay
{
    
    /**
     * @apiGroup 支付
     * @api {post} /api/app/call/createpayhref 获取支付链接
     * @apiParam {int}  money 钱数
     * @apiParam {string} pay_type 用于支付的类型,'weixin','zhifubao'
     * @apiSuccessExample 返回
     *
    {"code":0,"data":"https:\/\/openapi.alipay.com\/gateway.do?alipay_sdk=alipay-sdk-php-20161101&app_id=2019072565963731&biz_content={\"productCode\":\"QUICK_WAP_PAY\",\"body\":\"\",\"subject\":\"\u5145\u503c\",\"out_trade_no\":\"y202008240608129511005714\",\"total_amount\":\"0.01\",\"timeout_express\":\"1m\"}&charset=UTF-8&format=json&method=alipay.trade.wap.pay&notify_url=http:\/\/test.fuzhuangchangongxiao.cn\/home\/api\/notify_alipay&return_url=http:\/\/test.fuzhuangchangongxiao.cn\/home\/api\/success&sign_type=RSA2&timestamp=2020-08-24 18:50:13&version=1.0&sign=YkvDIpofX3sAX60oKejp%2F4DClDDToo68zZbTnkaXY0fikLXml%2Bo0XdHlgvvuTZhiQafgz2XP3qVXOV%2BFV5xVH4AAmhpt%2FwVqsGx2hPhbPRhzxxdnBIjfRLQ4yoypEONWUlcT2vuWnstGGZhdRfhhPXAdk28djYRPfuzXsgtUTIIIU3oAcpcT%2B4jYnPaJaw8n06bKEe%2FKD2IzCHbs56V5vvayZ90MEpho36AWyXpU9%2FgI9qfMNFWZjhiSRxISOibpsS1GRzV2FHEasn7HmT4ppiOGXgQR7fQLAv45VXHZs6XQqPiECNW9%2FjpdLd%2BcZBwjSM1C3XH8AKEcz7LyJS5BOg%3D%3D"}

     * @apiUse ERROR
     */
    public function createpayhref($post)
    {
        $play_type=$post['pay_type'];
        $money=$post['money'];
        $out_trade_no=MoneyPay::createPayNo(Yii::$app->user->id,$post['pay_type'],$post['money']);
        $pay=new PayService();
        $href=$pay->createOutTradeNo($play_type, $money,$out_trade_no);
        API::echo_json(0,$href);
    }


    /**
     * @apiGroup 支付
     * @api {post} /api/app/call/getUserMoney 获取当前用户的余额
     * @apiSuccessExample 返回
     *
    {"code":0,"data":"https:\/\/openapi.alipay.com\/gateway.do?alipay_sdk=alipay-sdk-php-20161101&app_id=2019072565963731&biz_content={\"productCode\":\"QUICK_WAP_PAY\",\"body\":\"\",\"subject\":\"\u5145\u503c\",\"out_trade_no\":\"y202008240608129511005714\",\"total_amount\":\"0.01\",\"timeout_express\":\"1m\"}&charset=UTF-8&format=json&method=alipay.trade.wap.pay&notify_url=http:\/\/test.fuzhuangchangongxiao.cn\/home\/api\/notify_alipay&return_url=http:\/\/test.fuzhuangchangongxiao.cn\/home\/api\/success&sign_type=RSA2&timestamp=2020-08-24 18:50:13&version=1.0&sign=YkvDIpofX3sAX60oKejp%2F4DClDDToo68zZbTnkaXY0fikLXml%2Bo0XdHlgvvuTZhiQafgz2XP3qVXOV%2BFV5xVH4AAmhpt%2FwVqsGx2hPhbPRhzxxdnBIjfRLQ4yoypEONWUlcT2vuWnstGGZhdRfhhPXAdk28djYRPfuzXsgtUTIIIU3oAcpcT%2B4jYnPaJaw8n06bKEe%2FKD2IzCHbs56V5vvayZ90MEpho36AWyXpU9%2FgI9qfMNFWZjhiSRxISOibpsS1GRzV2FHEasn7HmT4ppiOGXgQR7fQLAv45VXHZs6XQqPiECNW9%2FjpdLd%2BcZBwjSM1C3XH8AKEcz7LyJS5BOg%3D%3D"}

     * @apiUse ERROR
     */
    public function getUserMoney($post)
    {

        API::echo_json(0,['money'=>MoneyBalance::getUserMoney(Yii::$app->user->id)]);
    }



}